import numpy as np
import torch
from AiMonitor.dual_channel_detector import DualChannelDetector
from Monitor.structure_dna_extractor import StructureDNAExtractor
from Experiment.dna_mutation_analyzer import DNAMutationAnalyzer
from Experiment.fingerprint_visualizer import FingerprintVisualizer
import matplotlib.pyplot as plt

def simulate_bridge_case():
    """模拟桥梁损伤案例数据"""
    # 健康状态DNA（基准数据）
    healthy_dna = {
        'frequencies': [2.15, 5.30, 8.75],  # 单位Hz
        'damping': [0.018, 0.022, 0.025],
        'modes': [np.random.randn(30) for _ in range(3)]  # 30个测点的振型
    }

    # 引入损伤（刚度降低30%）
    damaged_dna = {
        'frequencies': [2.02, 5.08, 8.45],
        'damping': [0.025, 0.028, 0.031],
        'modes': [mode + 0.1 * np.random.randn(30) for mode in healthy_dna['modes']]
    }

    # 环境激励数据（随机振动）
    env_signal = np.random.randn(1000)

    return healthy_dna, damaged_dna, env_signal

if __name__ == '__main__':
    # === 案件重建 ===
    healthy_dna, damaged_dna, env_data = simulate_bridge_case()

    # === DNA提取 ===
    dna_extractor = StructureDNAExtractor(fs=1000)
    sample_dna = dna_extractor.extract_dna(env_data)

    # === 突变分析 ===
    dna_analyst = DNAMutationAnalyzer(healthy_dna)
    report = dna_analyst.analyze(sample_dna)
    print(f"突变评分：{report['score']:.2f}，热点参数：{report['hotspots']}")

    # === AI检测 ===
    model = DualChannelDetector()
    # 数据预处理（需转换为Tensor格式）
    dna_np = np.array([sample_dna['frequencies'], sample_dna['damping'], np.mean(sample_dna['modes'], axis=1)])
    dna_tensor = torch.tensor(dna_np, dtype=torch.float32).unsqueeze(0)
    env_tensor = torch.tensor(env_data, dtype=torch.float32).unsqueeze(0).unsqueeze(1)

    verdict = model(dna_tensor, env_tensor)
    print(f"损伤概率：{verdict[0][1].item() * 100:.1f}%")

    # === 可视化分析 ===
    visualizer = FingerprintVisualizer()
    fig = visualizer.compare_fingerprints(healthy_dna, sample_dna)
    plt.show()